﻿Public Class MapTableSelectForm

    Private _InternalFunction As Metis.Core.Functions.MetisInternalFunction
    Private _SelectedTable As Metis.Core.DB.DBTable

    Public Property [InternalFunction]() As Metis.Core.Functions.MetisInternalFunction
        Get
            Return _InternalFunction
        End Get
        Set(ByVal value As Metis.Core.Functions.MetisInternalFunction)
            _InternalFunction = value
            _SelectedTable = _InternalFunction.Table

            Dim OraReader As OracleDataReader
            Dim OraCmd As OracleCommand
            Dim TblName As String = _SelectedTable.TableName
            If TblName = "" Then TblName = "<empty>"
            OraCmd = New OracleCommand(String.Format("SELECT dbtbl.table_name " & _
                                       "FROM user_tables dbtbl, (SELECT * FROM dam_functions WHERE table_name<>'{0}') damtbl " & _
                                       "WHERE NOT dbtbl.table_name LIKE 'DAM%' " & _
                                       "AND dbtbl.table_name=damtbl.table_name(+) " & _
                                       "AND damtbl.table_name IS NULL ORDER BY dbtbl.table_name", TblName), _InternalFunction.MetisCore.Connection)

            OraReader = OraCmd.ExecuteReader
            lstDBTables.Items.Clear()
            Do While OraReader.Read
                lstDBTables.Items.Add(OraReader.GetString(0))
            Loop
            OraReader.Close()
            btnOK.Enabled = lstDBTables.Items.Count > 0
        End Set
    End Property

    Public ReadOnly Property SelectedTable As Metis.Core.DB.DBTable
        Get
            Return _SelectedTable
        End Get
    End Property

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click, lstDBTables.DoubleClick
        If btnOK.Enabled And lstDBTables.SelectedIndex >= 0 Then
            _SelectedTable = New Metis.Core.DB.DBTable(_InternalFunction.MetisCore, lstDBTables.SelectedItem, Core.DB.DBTableType.Table, Core.MetisLoadState.NotLoaded)
        End If
    End Sub
End Class